﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SantaSLittleIrcHelper.Registration
{

    public enum currentState { Nothing = 0, NicknameAvailable = 1, NicknameRegistered = 2, RegistrationConfirmed = 3 }
    public enum lastActionDone { 
        Nothing = 0, 
        NicknameIsAvailable = 1, 
        NicknameIsNotAvailable = 2, 
        NicknameSuccessfulRegistered = 3, 
        NicknameNotSuccessfulRegisteredNicknameInUse = 4, 
        NicknameNotSuccessfulRegisteredInvalidEmail = 5,
        NicknameNotSuccessfulRegisteredDataMissing = 6, 
        NicknameNotSuccessfulRegisteredInvalidPassword = 7, 
        NicknameSuccessfulConfirmed = 8, 
        NicknameNotSuccessfulConfirmedMissingUsername = 9, 
        NicknameNotSuccessfulConfirmedMissingConfirmationCode = 10, 
        NicknameNotSuccessfulRegisteredNicknameAlreadyRequested = 11, 
        NicknameNotSuccessfulRegisteredNicknameAlreadyRegistered = 12,
        NicknameNotSuccessfulConfirmedNicknameInUse = 13,
        NicknameNotSuccessfulConfirmedPasscodeInvalid = 14,
    }
    public enum assistantLabels { State, Error }
    public delegate void SetAssistantMessage(String name, String[] paramX, assistantLabels label);

    class RegistrationAssistant
    {

        private static RegistrationAssistant instance = null;
        private static readonly object padlock = new object();
        private currentState currentState = 0;

        public static RegistrationAssistant Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new RegistrationAssistant();
                    }
                    return instance;
                }
            }
        }

        private RegistrationAssistant()
        {


        }

        public void invokeChangeText(String name, assistantLabels label, params String[] paramX)
        {
            SetAssistantMessage foo = new SetAssistantMessage(invokedChangeText);
            Program.instance.GUI.Invoke(foo, new object[] { name, paramX, label });
        }

        public void invokedChangeText(String name, String[] paramX, assistantLabels label)
        {
            TLabel x = (TLabel)Program.instance.GUI.Controls.Find("assistant" + label.ToString() + "Label", true)[0];
            x.Key = name;
            x.Args = paramX;
        }

        public void stateChanged(lastActionDone last)
        {

            switch (last)
            {
                case lastActionDone.NicknameIsAvailable:
                    this.currentState = currentState.NicknameAvailable;
                    this.invokeChangeText("assistantNicknameCanBeRegistered", assistantLabels.State, Program.instance.settings.nsUsername);
                    this.invokeChangeText(null, assistantLabels.Error);
                    break;

                case lastActionDone.NicknameIsNotAvailable:
                    this.invokeChangeText("assistantNicknameIsInUse", assistantLabels.Error, Program.instance.settings.nsUsername);
                    break;

                case lastActionDone.NicknameSuccessfulRegistered:
                    this.currentState = currentState.NicknameRegistered;
                    this.invokeChangeText("assistantNicknameIsRegistered", assistantLabels.State, Program.instance.settings.nsUsername);
                    this.invokeChangeText(null, assistantLabels.Error);
                    break;

                case lastActionDone.NicknameSuccessfulConfirmed:
                    this.currentState = currentState.RegistrationConfirmed;
                    this.invokeChangeText("assistantNicknameSuccessfulConfirmed", assistantLabels.State, Program.instance.settings.nsUsername);
                    this.invokeChangeText(null, assistantLabels.Error);
                    break;

                case lastActionDone.NicknameNotSuccessfulRegisteredNicknameInUse:
                case lastActionDone.NicknameNotSuccessfulRegisteredInvalidEmail:
                case lastActionDone.NicknameNotSuccessfulRegisteredDataMissing:
                case lastActionDone.NicknameNotSuccessfulRegisteredInvalidPassword:
                case lastActionDone.NicknameNotSuccessfulConfirmedMissingConfirmationCode:
                case lastActionDone.NicknameNotSuccessfulConfirmedMissingUsername:
                case lastActionDone.NicknameNotSuccessfulRegisteredNicknameAlreadyRequested:
                case lastActionDone.NicknameNotSuccessfulRegisteredNicknameAlreadyRegistered:
                case lastActionDone.NicknameNotSuccessfulConfirmedNicknameInUse:
                case lastActionDone.NicknameNotSuccessfulConfirmedPasscodeInvalid:
                    this.invokeChangeText("assistant" + last.ToString(), assistantLabels.Error, Program.instance.settings.nsUsername);
                    break;

            }

        }
    }
}
